package sanbing.jcpplearning;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class ByteBufExample {
    public static void main(String[] args) {
        // 创建一个初始容量为 10 的非池化 ByteBuf
        ByteBuf buffer = Unpooled.buffer(10);

        System.out.println("Initial State:");
        printBufferState(buffer);

        // 写入数据
        buffer.writeBytes(new byte[]{1, 2, 3, 4, 5});
        System.out.println("\nAfter Writing 5 Bytes:");
        printBufferState(buffer);

        // 标记当前写指针
        buffer.markWriterIndex();
        System.out.println("\nMark Writer Index");

        // 再写入数据
        buffer.writeBytes(new byte[]{6, 7});
        System.out.println("\nAfter Writing 2 More Bytes:");
        printBufferState(buffer);

        // 重置写指针到标记位置
        buffer.resetWriterIndex();
        System.out.println("\nAfter Resetting Writer Index:");
        printBufferState(buffer);

        // 读取数据
        byte firstByte = buffer.readByte();
        System.out.println("\nRead First Byte: " + firstByte);
        printBufferState(buffer);

        // 标记当前读指针
        buffer.markReaderIndex();
        System.out.println("\nMark Reader Index");

        // 读取两个字节
        byte secondByte = buffer.readByte();
        byte thirdByte = buffer.readByte();
        System.out.println("Read Second Byte: " + secondByte);
        System.out.println("Read Third Byte: " + thirdByte);
        printBufferState(buffer);

        // 重置读指针到标记位置
        buffer.resetReaderIndex();
        System.out.println("\nAfter Resetting Reader Index:");
        printBufferState(buffer);

        // 丢弃已读数据
        buffer.discardReadBytes();
        System.out.println("\nAfter Discarding Read Bytes:");
        printBufferState(buffer);

        // 检查可读字节数
        System.out.println("\nReadable Bytes: " + buffer.readableBytes());

        // 检查可写字节数
        System.out.println("Writable Bytes: " + buffer.writableBytes());

        // 动态扩容
        buffer.writeBytes(new byte[]{8, 9, 10, 11, 12, 13});
        System.out.println("\nAfter Writing Bytes Beyond Initial Capacity:");
        printBufferState(buffer);

        // 清空缓冲区（仅重置指针，不清空内容）
        buffer.clear();
        System.out.println("\nAfter Clearing Buffer:");
        printBufferState(buffer);

        // 引用计数操作
        System.out.println("\nInitial Reference Count: " + buffer.refCnt());
        buffer.retain();
        System.out.println("After Retain: " + buffer.refCnt());
        buffer.release();
        System.out.println("After First Release: " + buffer.refCnt());
        buffer.release();
        System.out.println("After Final Release: " + buffer.refCnt());
    }

    /**
     * 打印缓冲区的状态，包括 readerIndex、writerIndex、capacity 和内容。
     */
    private static void printBufferState(ByteBuf buffer) {
        System.out.println("readerIndex: " + buffer.readerIndex());
        System.out.println("writerIndex: " + buffer.writerIndex());
        System.out.println("capacity: " + buffer.capacity());
        System.out.println("readableBytes: " + buffer.readableBytes());
        System.out.println("writableBytes: " + buffer.writableBytes());
        System.out.print("Buffer Content: ");
        for (int i = buffer.readerIndex(); i < buffer.writerIndex(); i++) {
            System.out.print(buffer.getByte(i) + " ");
        }
        System.out.println();
    }
}